﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using WebApplication2.Model;
using WebApplication2.Server;
using WebApplication2.Util;

namespace WebApplication2.Handler
{
    /// <summary>
    /// 统计数据的Handler
    /// </summary>
    public class OutputValueHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
            string funC = context.Request["func"];
            try
            {
                switch (funC)
                {
                    case "addProject":
                        //addProject(context);
                        break;
                    case "queryAllProject":
                    //    queryAllProject(1, context);
                        break;

                    case "year":
                        year(context);
                        break;
                    default:
                        context.Response.Write("{data:\"参数错误！\"}");
                        Console.WriteLine("nihao,参数错误！");
                        break;
                }
            }
            catch (Exception e)
            {
                context.Response.ClearContent();
                context.Response.Write(e.ToString());
            }
        }

        ////统计功能用算法，首先求得年份的最大最小值，
        ////然后循环使用sql求得各个年份的数据,
        ////再将output对象append到List<Output> 中
        public void year(HttpContext context)
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            Output output = new Output();
            List<Output> list = new List<Output>();
            //一个List<Output>的feedBack
            FeedBackMsg<List<Output>> feedBack = new FeedBackMsg<List<Output>>();
            string sql = string.Empty;
            int max = Getyear().Max();
            int min = Getyear().Min();
            //逐年循环查询
            //for (int i = min - 1; i < max+2; i++)
            //{
            //    output.Year = i;
            //    sql = "select * from T_ProjectInfo where YEAR(F_finishe) =" + i + " ";
            //    DataRowCollection rows = MrDBAccess.ExecuteDataSet(sql).Tables[0].Rows;
            //    int iRowLength = rows.Count;
            //    //对产值Value进行累加
            //    if (iRowLength > 0)
            //    {
            //        for (int j = 0; j < iRowLength; j++)
            //        {
            //            output.Value = DataBase.ObjectToInt(rows[j]["F_Budge"])+output.Value;
            //        }
            //    }
            //    list.Add(output);
            //    //将output置空，循环使用
            //    output.Year = 0;
            //    output.Value = 0; 
            //}
            feedBack.Code = 1;
            feedBack.Msg = "年度统计数据";
            feedBack.Obj = list;
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }



        public List<Int32> Getyear() {
            List<String> Stryear = new List<string>();
            List<Int32> year = new List<Int32>();
            string sql = string.Empty;
            sql = "SELECT * from T_ProjectInfo";
            DataRowCollection rows = MrDBAccess.ExecuteDataSet(sql).Tables[0].Rows;
            if (rows != null && rows.Count > 0)
            {
                int iRowLength = rows.Count;
                for (int i = 0; i < iRowLength; i++)
                {
                    Stryear.Add(DataBase.ObjectToString(rows[i]["F_Finishe"]));
                }
            }
            foreach (String item in Stryear)
            {
                DateTime time = Convert.ToDateTime(item);
                year.Add(time.Year);
            }

            return year;
         
        }

        public void mouth() 
        {
        
        
        }

        public void season() 
        {
        
        
        
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}